/*                   _   _ _ __                               *\
**     ________ ___ | | | | '_ \   scaup - Scala up!          **
**    / __/ __// _ || | | | |_) |  (c) 2008, Normen Mueller   **
**  __\ \/ /__/ __ || |_| | .__/   http://kwarc.info.nmueller **
** /____/\___/_/ |_/ \__,_|_|                                 **
**                                                            **
\*                                                            */
  
// $Id: Reporter.scala 9 2008-05-24 12:12:30Z normen.mueller $

/* This class has its origin at: scala.tools.nsc.reporters.Reporter */

package scaup.reporter

import scaup.io.fs.SourceFile

/** This interface provides methods to issue information, warning and error messages. */
abstract class Reporter {
  object severity extends Enumeration
  
  abstract class Severity extends severity.Value {
    var count: Int = 0
  }
  
  object INFO    extends Severity { def id = 0 }
  object WARNING extends Severity { def id = 1 }
  object ERROR   extends Severity { def id = 2 }

  def reset: Unit = {
    INFO.count = 0
    ERROR.count   = 0
    WARNING.count = 0
  }

  def hasErrors: Boolean = ERROR.count != 0

  /** Flush all output */
  def flush() { }
  
  protected def info0(msg: String, severity: Severity, force: Boolean): Unit
  
  private var source: SourceFile = _
  def setSource(source: SourceFile) { this.source = source }
  def getSource: SourceFile = source

  def    info(msg: String, force: Boolean): Unit = info0(msg,    INFO, force)
  def warning(msg: String                ): Unit = info0(msg, WARNING, false)
  def   error(msg: String                ): Unit = info0(msg,   ERROR, false)
  
  /** Returns a string meaning "n elements". */
  def countElementsAsString(n: Int, elements: String): String = n match {
    case 0 => "no "    + elements + "s"
    case 1 => "one "   + elements
    case 2 => "two "   + elements + "s"
    case 3 => "three " + elements + "s"
    case 4 => "four "  + elements + "s"
    case _ => "" + n + " " + elements + "s"
  }
    
  /** Turns a count into a friendly English description if n<=4. */
  def countAsString(n: Int): String = n match {
    case 0 => "none"
    case 1 => "one"
    case 2 => "two"
    case 3 => "three"
    case 4 => "four"
    case _ => "" + n
  }
}
